<?php
namespace app\api\controller;

use app\common\controller\Common;
use org\ChapterPage;
use org\Page;
use org\SiteMap;
use think\Db;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Env;
use think\facade\Request;
use think\helper\Time;

class V1 extends Common
{
    /**
     * 区块接口
     */
    public function block ()
    {
        $block_id = (int)$this->request->param("block_id",0);
        $limit = (int)$this->request->param("limit",10);
        return $this->echoJson(get_block_list($block_id,$limit));
    }
    /**
     * 章节内容
     * @throws
     */
    public function chapter_content()
    {
        $primary_id = (int)$this->request->param("primary_id",0);
        $chapter_id = (int)$this->request->param("chapter_id",0);
        $flag = $this->request->param("flag","custom");

        $article = model('article')->where('PrimaryId','eq',$primary_id)->cache()->find()->toArray();

        $chapter = get_chapter_flag_details($flag,$article['_id'],$chapter_id);

        $chapter_data = curl_server("api_v1/spider_chapter_content",array(
            'article_id' => json_decode(json_encode($article['_id']),true)['$oid'],
            'sort' => $chapter['Sort'],
            'chapter_url' => $chapter['Url'],
        ),"http://127.0.0.1:1122");

        $chapter['Content'] = trim(preg_replace('/<\/*div.*?>/u',"",$chapter_data['data']));

        return $this->echoJson($chapter);
    }
    /**
     * 章节列表
     */
    public function chapter_list()
    {
        $primary_id = (int)$this->request->param("primary_id",0);
        $flag = $this->request->param("flag","custom");

        $article = model('article')->where('PrimaryId','eq',$primary_id)->cache()->find()->toArray();
        $chapter_list = get_flag_chapter_list($article['_id'],$flag);

        foreach ($chapter_list as $key => $item) {
            $chapter_list[$key]['PrimaryId'] = $primary_id;
            $chapter_list[$key]['Flag'] = $flag;
        }

        return $this->echoJson($chapter_list);
    }

    /**
     * 小说详情
     * @throws
     */
    public function article()
    {
        $primary_id = (int)$this->request->param("primary_id",0);

        $article = model('article')->where('PrimaryId','eq',$primary_id)->cache()->find()->toArray();
        $all_chapter_list = get_all_chapter_list($article['_id'],$article['SourceList'],0,0);

        $article['SourceUrl'] = "";
        //获取小说点击量
        $article['Click'] = get_novel_click($article['PrimaryId']);

        //获取当前源地址
        foreach ($article['SourceList'] as $url) {
            if (get_flag_name($url) == $article['LastChapterFlag']){
                $article['SourceUrl'] = $url;
                break;
            }
        }

        //修复小说表没有 LastChapterFlag 标记
        if ($article['SourceUrl'] == "" && count($article['SourceList']) > 0) {
            $article['SourceUrl'] = $article['SourceList'][0];
        }

        //获取当前分类
        $article['Category'] = get_category($article['Cid']);

        $article['LastUpdateChapterList'] = $all_chapter_list['last_update_chapter_list'];

        //最后更新章节id和标题
        $article['LastChapterSort'] = $all_chapter_list['last_chapter_sort'];
        $article['LastChapterTitle'] = $all_chapter_list['last_chapter_title'];
        $article['LastChapterFlag'] = $all_chapter_list['last_chapter_flag'];

        //排行榜计数
        add_redis_cid_ranking($article['PrimaryId'],$article['Category']['cid'],"top");
        //设置小说点击量
        set_novel_click($article['PrimaryId']);

        return $this->echoJson($article);
    }

    /**
     * 搜索接口
     */
    public function search()
    {
        $keyword = $this->request->param("keyword","");
        $list = model("article")->whereIn("Cid",get_all_sub_cid_list())->whereOr('Title','like',trim($keyword))->whereOr('Author','like',trim($keyword))->order('UpdateTime','desc')->cache()->select()->toArray();
        return $this->echoJson($list);
    }

    /**
     * 输出分类
     */
    public function category_list()
    {
        return $this->echoJson($this->config['category_list']);
    }

    /**
     * 输出分类小说
     * @return mixed
     * @throws
     */
    public function classify_list()
    {
        // 完结类型 可选参数 2 全部  0 未完结  1 已完结
        $full_type = (int)$this->request->param("full_type",2);
        // 分类id
        $cid = (int)$this->request->param("cid",0);
        // 当前页码
        $page = (int)$this->request->param("page",1);
        // 页码大小
        $page_size = (int)$this->request->param("page_size",20);
        // 时间类型 可选参数  0  全部   1  今天  3 三日内  7 七日内   15 半月内   30 一月内
        $time_type =(int)$this->request->param("time_type",0);

        //分页计算
        $offset = ($page - 1) * $page_size;

        $where = [];
        //分类选中
        if ($cid != 0) {
            $where[] = ['Cid','in',get_sub_cid_list($cid)];
        }else {
            $where[] = ['Cid','in',get_all_sub_cid_list()];
        }

        //连载状态
        if ($full_type != 2) {
            $where[] = ['Full','eq',$full_type];
        }

        //更新时间
        if ($time_type != 0) {
            $update_time = 0;
            switch ($time_type) {
                case 1 :
                    $update_time = Time::daysAgo(1);
                    break;
                case 3 :
                    $update_time = Time::daysAgo(3);
                    break;
                case 7 :
                    $update_time = Time::daysAgo(7);
                    break;
                case 15 :
                    $update_time = Time::daysAgo(15);
                    break;
                case 30 :
                    $update_time = Time::daysAgo(30);
                    break;
            }

            if ($update_time != 0) {
                $where[] = ['UpdateTime','gt',$update_time];
            }
        }

        $model = model("article")->where($where);
        $total = $model->count();

        $list = $model->limit($offset,$page_size)->order('UpdateTime','DESC')->cache()->select()->toArray();

        return $this->echoJson(array(
            'total' => $total,
            'list' => $list
        ));

    }

}
